package com.spirit.mypopup.menu;

import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.spirit.mypopup.R;
import com.spirit.mypopup.adapter.NewQAdapter;
import com.spirit.mypopup.menu.adapter.BaseMenuFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * author : fang.zhang
 * e-mail : initzf@126.com
 * date   : 2020/5/16
 * desc   :
 */
public class FragmentFlite extends BaseMenuFragment {
    private static final String TAG = "FragmentFlite";

    public static FragmentFlite newInstance(List<? extends OptionObject> optionObj) {

        Bundle args = new Bundle();
        args.putParcelableArrayList("obj", (ArrayList<? extends Parcelable>) optionObj);
        FragmentFlite fragment = new FragmentFlite();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_filte_layout, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ListView mListView = view.findViewById(R.id.mList);

        List<? extends OptionObject> optionObj = getArguments().getParcelableArrayList("obj");

        NewQAdapter adapter = new NewQAdapter(getContext(), optionObj);

        mListView.setAdapter(adapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setPosition(position);
                if (onSelectItemListener != null) {
                    onSelectItemListener.onSelectItemListener(adapter.getItem(position));
                }
            }
        });
    }
}
